﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
/// <summary>
///AutoFormFactory 的摘要说明
/// </summary>
public class AutoFormFactory
{
    public static IAutoForm GetAutoFormCreator(Type t)
    {        
        IAutoForm inter= FactoryCreator.GetObjectFromFactory<IAutoForm>(t.Name + "FormField");

        if (inter == null)
        {
            return new StringFormField();
        }
        return inter;
    }

    public static IAutoForm GetAutoFormCreator(PropertyInfo pinfo)
    {
        IAutoForm inter = null;
        if (pinfo.GetCustomAttributes(typeof(AutoFormDisplayTypeAttribute),false).Length>0)
        {
            inter = ((AutoFormDisplayTypeAttribute)pinfo.GetCustomAttributes(typeof(AutoFormDisplayTypeAttribute), false).First()).DisplayTypeObject;
        }
        else if (pinfo.PropertyType.IsEnum)
        {
            inter = new EnumFormField();
        }
        else if (pinfo.PropertyType.IsArray)
        {
            inter = new ArrayFormField();
        }
        else
        {
            inter = AutoFormFactory.GetAutoFormCreator(pinfo.PropertyType);
        }

        return inter;
    }
}
